package com.li.youhappygo.controller;

import com.github.pagehelper.PageInfo;
import com.li.youhappygo.annotation.Log;
import com.li.youhappygo.annotation.OperatorType;
import com.li.youhappygo.model.dto.ProductDto;
import com.li.youhappygo.model.dto.ProductQueryParamDto;
import com.li.youhappygo.model.entity.product.Product;
import com.li.youhappygo.model.vo.common.Result;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.model.vo.product.ProductVo;
import com.li.youhappygo.service.ProductService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@Tag(name = "商品管理", description = "商品管理")
public class ProductController {
    @Autowired
    ProductService productService;

    @GetMapping("/getProductList/{pageNum}/{pageSize}")
    @Operation(description = "分页查询商品信息")
    @Transactional
    public Result<PageInfo<ProductVo>> getProductList(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageInfo<ProductVo> pageInfo = productService.getProductList(pageNum,pageSize);
        return Result.build(pageInfo, ResultCode.SUCCESS);
}
    @PostMapping("/insertProduct")
    @Operation(summary = "插入一个商品",description = "插入一个商品")
//    @Log(title = "插入一个商品", businessType = 1,operatorType = OperatorType.MANAGE)
    public Result save(@RequestBody ProductDto productDto) {
        productService.save(productDto);
        return Result.build(null , ResultCode.SUCCESS) ;
    }



    @PutMapping("/updateById")
    @Operation(summary = "更新一个商品",description = "更新一个商品")
//    @Log(title = "更新一个商品", businessType = 2,operatorType = OperatorType.MANAGE)
    @Transactional
    public Result updateById(@Parameter(name = "product", description = "请求参数实体类", required = true) @RequestBody ProductDto productDto) {
        productService.updateById(productDto);
        return Result.build(null , ResultCode.SUCCESS) ;
    }
    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "删除一个商品",description = "删除一个商品")
    @Log(title = "删除一个商品", businessType = 3,operatorType = OperatorType.MANAGE)
    public Result deleteById(@Parameter(name = "id", description = "商品id", required = true) @PathVariable Long id) {
        productService.deleteById(id);
        return Result.build(null , ResultCode.SUCCESS) ;
    }
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    @Operation(summary = "更新商品的审核状态",description = "更新审核状态")
    @Log(title = "更新商品的审核状态", businessType = 2,operatorType = OperatorType.MANAGE)
    public Result updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return Result.build(null , ResultCode.SUCCESS) ;
    }
    @GetMapping("/updateStatus/{id}/{status}")
    @Operation(summary = "更新商品的上架状态",description = "更新状态")
    @Log(title = "更新商品的上架状态", businessType = 2,operatorType = OperatorType.MANAGE)
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return Result.build(null , ResultCode.SUCCESS) ;
    }

    @GetMapping("/getProductByBrandId/{brandId}")
    @Operation(summary = "根据品牌ID查找商品")
    public Result<List<Product>> getProductByBrandId(@PathVariable Long brandId) {
        List<Product> productList = productService.getProductByBrandId(brandId);
        return Result.build(productList , ResultCode.SUCCESS) ;
    }

    @GetMapping("getProductByAuditStatus/{audit_status}")
    @Operation(summary = "根据审核状态查找商品",description = "根据审批状态筛选出商品")
    public Result<List<Product>> getProductByAuditStatus(@PathVariable Integer audit_status) {
        List<Product> productList = productService.getProductByAuditStatus(audit_status);
        return Result.build(productList , ResultCode.SUCCESS) ;
    }

    @GetMapping("getProductByCategoryId/{categoryId}")
    @Operation(summary = "根据分类ID查找商品",description = "根据分类ID查找商品")
    public Result<List<Product>> getProductByCategoryId(@PathVariable Long categoryId) {
        List<Product> products = productService.getProductByCategoryId(categoryId);
        return Result.build(products , ResultCode.SUCCESS) ;
    }

    @PostMapping("getProductListByQueryParam")
    @Operation(summary = "根据查询条件查找商品",description = "根据查询条件查找商品")
    public Result<PageInfo<ProductVo>> getProductListByQueryParam(@RequestBody ProductQueryParamDto productQueryParamDto,@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageInfo<ProductVo>  pageInfo = productService.getProductListByQueryParam(productQueryParamDto,pageNum,pageSize);
        return Result.build(pageInfo , ResultCode.SUCCESS) ;
    }

    @Operation(summary = "批量删除商品",description = "批量删除商品")
    @Log(title = "批量删除商品", businessType = 3,operatorType = OperatorType.MANAGE)
    @PostMapping("update/deleteStatus")
    public Result updateDeleteStatus(@RequestBody List<Long> ids) {
        productService.updateDeleteStatus(ids);
        return Result.build(null , ResultCode.SUCCESS) ;
    }

    @Operation(summary = "批量更新商品审核状态",description = "批量更新商品审核状态")
    @PostMapping("update/auditStatus")
    public Result updateAuditStatus(@RequestBody List<Long> ids,@RequestParam Integer auditStatus) {
        productService.updateAuditStatusByList(ids,auditStatus);
        return Result.build(null , ResultCode.SUCCESS) ;
    }




}
